package com.yueyang.sojourn.server.service.attachdetail;

import com.yueyang.sojourn.server.config.QiniuProperties;
import com.yueyang.sojourn.server.model.dto.AttachDto;
import com.yueyang.sojourn.server.model.enums.StatusEnum;
import com.yueyang.sojourn.server.model.po.AttachDetail;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author yemingxing
 * @summary
 * @Copyright (c) 2020, Yueyang Group All Rights Reserved.
 * @since 2020-02-12
 */
@Service
public class AttachDetailService {

    @Resource
    private QiniuProperties qiniuProperties;

    @Resource
    private AttachDetailMapper attachDetailMapper;


    public int deleteByPrimaryKey(Long id) {
        return attachDetailMapper.deleteByPrimaryKey(id);
    }

    public int insert(AttachDetail record) {
        return attachDetailMapper.insert(record);
    }

    public int insertSelective(AttachDetail record) {
        return attachDetailMapper.insertSelective(record);
    }

    public void insertList(List<AttachDetail> records) {
        attachDetailMapper.insertList(records);
    }

    public AttachDetail selectByPrimaryKey(Long id) {
        AttachDetail attachDetail = attachDetailMapper.selectByPrimaryKey(id);
        formatPublicFileUrl(attachDetail);
        return attachDetail;
    }

    public int updateByPrimaryKeySelective(AttachDetail record) {
        return attachDetailMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(AttachDetail record) {
        return attachDetailMapper.updateByPrimaryKey(record);
    }

    //================================basic service====================================================//

    /**
     * 新增附件
     *
     * @param loginUserId
     * @param attachDto
     * @return 主键id
     */
    public long addReturnId(long loginUserId, AttachDto attachDto) {
        AttachDetail attachDetail = new AttachDetail();
        BeanUtils.copyProperties(attachDto, attachDetail);
        attachDetail.setStatus(StatusEnum.VALID.getCode());
        attachDetail.setCreator(loginUserId);
        attachDetail.setCreatedTime(new Date());
        this.insert(attachDetail);
        return attachDetail.getId();
    }

    public List<AttachDetail> selectListByProjectId(Long sojournProjectId) {
        List<AttachDetail> attachDetails = attachDetailMapper.selectListByProjectId(sojournProjectId);
        attachDetails.parallelStream().forEach(this::formatPublicFileUrl);
        return attachDetails;
    }

    public List<AttachDetail> selectListByMerchId(Long sojournMerchId) {
        List<AttachDetail> attachDetails = attachDetailMapper.selectListByMerchId(sojournMerchId);
        attachDetails.parallelStream().forEach(this::formatPublicFileUrl);
        return attachDetails;
    }

    private void formatPublicFileUrl(AttachDetail attachDetail) {
        attachDetail.setFileUrl(qiniuProperties.getDomain() + "/" + attachDetail.getName());
    }

    public List<AttachDetail> selectListByIds(List<Long> ids) {
        List<AttachDetail> attachDetails = attachDetailMapper.selectListByIds(ids);
        attachDetails.parallelStream().forEach(this::formatPublicFileUrl);
        return attachDetails;
    }
}
